from django.shortcuts import render
from django.views import View
from . import forms
from django.http import JsonResponse,HttpResponse
from . import models
# from django.forms import model_to_dict
from common.utils import model_to_dict
from . import const
from django.core.paginator import Paginator
from django.db.models import Q
from common.custome_response import FlyResponse
import json

# Create your views here.
class GradeView(View):

    def post(self,request):
        form = forms.GradeForm(request.POST)
        if form.is_valid():
            form.save() #直接能保存到数据库
            return FlyResponse()

        return FlyResponse(code=-1, msg=form.error_msg)

    def get(self, request):
        limit = request.GET.get("limit", const.limit)
        page = request.GET.get("page", 1)
        search=request.GET.get('search')
        teacher=request.GET.get('teacher')

        data_list = []
        if teacher and search:
            query_set=models.Grade.objects.filter(teacher=teacher).filter(Q(name__contains=search) | Q(teacher__contains=search))

        elif teacher:
            query_set=models.Grade.objects.filter(teacher=teacher)

        elif search:
            query_set = models.Grade.objects.filter(Q(name__contains=search) | Q(teacher__contains=search))

        else:
            query_set = models.Grade.objects.all()


        paginator = Paginator(query_set, limit)
        page_data = paginator.page(page)
        print(paginator.count)
        for query in page_data:
            data_list.append(model_to_dict(query))

        return FlyResponse(data=data_list, count=paginator.count)

    def delete(self,request):
        id=request.GET.get("id")
        if not id:
            return FlyResponse(-1,"请传入要删除的id")

        obj=models.Grade.objects.filter(id=id)
        if not obj.exists():
            # ret = {"code": -1, "msg": "要删除的数据不存在"}
            # return JsonResponse(ret)
            return FlyResponse(-1,"要删除的数据不存在 ")
        obj.delete()
        # ret={"code":-1,"msg":"删除成功"}
        # return JsonResponse(ret)
        return FlyResponse()

    def put(self,request):#修改数据
        # print(request.GET)
        # print(request.POST)
        print("request.PUT",request.PUT)
        print(request.PUT.get("id"))
        print("request.FILES",request.FILES)
        id=request.PUT.get("id")
        if not id:
            # ret={"code":-1,"msg":"请传入要修改的id"}
            # return HttpResponse("ret")
            return FlyResponse(-1,"请传入要修改的id")
        obj=models.Grade.objects.filter(id=id)
        if not obj.exists():
            # ret={"code": -1, "msg": "要修改的数据不存在"}
            # return JsonResponse(ret)
            return FlyResponse(-1,"要修改的id不存在")

        form=forms.GradeForm(request.PUT,instance=obj.first())
        if form.is_valid():
            form.save()
            # ret={"code":0,"msg":"操作成功"}
            # return JsonResponse(ret)
            return FlyResponse()

        # ret={"code":-1,"msg":form.error_msg}
        # return JsonResponse(ret)
        return FlyResponse(code=-1,msg=form.error_msg)

